<?php
/**
 * Description of AdminAction
 *
 * @author Magno
 */
class AdminAction extends ActionDefault{

    private $adminService;
    private $statusService;
    private $mediaService;

    public static $VIEW_ADMIN = "view/admin/";

    function __construct() {
        parent::__construct();
        $this->setValueOutput("adminMenu", "current");
        $this->adminService = new AdminService();
        $this->statusService = new StatusService();
        $this->mediaService = new MediaService();
    }
    
    public function getView() {
        return AdminAction::$VIEW_ADMIN;
    }

    public function buscar() {

    }

    public function cadastro() {       
    }

    public function detalhes($adminID) {

    }
    public function editar($adminID) {
        try{

            if($adminID <= 0){
                parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, "Código do Administrador Não Encontrado!!!"));
                parent::voltar();
                return;
            }

            $adminBean = $this->adminService->buscarPorID($adminID);

            if($adminBean == null){
                parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, "Administrador Não Encontrado!!!"));
                parent::voltar();
                return;
            }
            
            parent::setObjectOutput($adminBean->getUsuario());
            parent::show(AdminAction::$VIEW_ADMIN."cadastro.tpl");

        }catch(Exception $ex){
            parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, $ex->getMessage()));            
            parent::voltar();
        }
    }

    public function excluir($adminID) {
        try{

            if($adminID <= 0){
                parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, "Código do Administrador Não Encontrado!!!"));
                parent::voltar();
                return;
            }

            $adminBean = $this->adminService->buscarPorID($adminID, true);

            if($adminBean == null){
                parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, "Administrador Não Encontrado!!!"));
                parent::voltar();
                return;
            }

            $adminBean->setStatus($this->statusService->buscarPorID(Constantes::$ST_USU_EXCLUIDO));

            $this->adminService->salvar($adminBean);

            parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_SUCCESS, Constantes::$STR_MSG_SUCCESS));
            parent::chain('AdminAction.listar');

        }catch(Exception $ex){
            parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, $ex->getMessage()));
            parent::voltar();
        }
    }

    public function listar() {
        parent::setValueOutput('status', Util::arrayCombo($this->statusService->listar(0, Constantes::$STATUS_USUARIO), 'statusID', 'descricao',' -- TODOS --'));
        $this->buscaListaPaginada();
    }

    public function tabela($pagina = 1, $email = "", $login = "", $statusID = -1) {
        $this->buscaListaPaginada($pagina, $email, $login, $statusID);
        parent::show('view/usuario/tabela.tpl');
    }
    
    public function salvar($usuarioBean) {
        try{

            $uploadService = new UploadService();

            $statusBean = $this->statusService->buscarPorID(Constantes::$ST_USU_CADASTRADO);
            $mediaBean = new MediaBean();

            $caminhoMedia = UploadService::salvarArquivoSimples('media',Constantes::$DIR_IMAGENS);

            if(strlen($caminhoMedia) <= 0){
                $caminhoMedia = Constantes::$DIR_UPLOAD.Constantes::$DIR_IMAGENS.Constantes::$IMG_ADM_DEFAULT;
            }

            $mediaBean->setCaminho($caminhoMedia);

            $usuarioBean->setStatus($statusBean);
            $usuarioBean->setMedia($mediaBean);

            $adminBean = new AdminBean();
            $adminBean->setUsuario($usuarioBean);
            
            $this->adminService->salvar($adminBean);

            parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_SUCCESS, Constantes::$STR_MSG_SUCCESS));
            parent::chain('AdminAction.listar');
            
        }catch(Exception $ex){
            parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, $ex->getMessage()));
            parent::voltar();            
        }
    }

    private function buscaListaPaginada($pagina = 1, $email = "", $login = "", $statusID = -1){

        $admins = $this->adminService->listar($pagina, $email, $login, $statusID, true);
        $total = $this->adminService->total($email, $login, $statusID);

        parent::setValueOutput('admins', $admins);
        parent::setValueOutput('pagina', $pagina); 
        parent::setValueOutput('numPaginas', Util::calculaNumPaginas($total));
        
    }
}
?>
